package com.company.project.wechat.commom;

import com.company.project.wechat.officialAccounts.entity.PicNews;
import com.company.project.wechat.officialAccounts.entity.PicNewsMessage;
import com.company.project.wechat.officialAccounts.entity.TextMessage;
import com.thoughtworks.xstream.XStream;

import java.util.List;

/**
 * 创建各种信息Utils
 * @author ZYF
 * @date 2021/5/18 11:03
 */
public class ReplyMessageUtil {

    /**
     * 创建文本信息
     * @author zyf
     * @param ToUserName
     * @param fromUserName
     * @param Content
     * @return
     */
    public static String initTextMessage(String ToUserName,String fromUserName,String Content) {
        TextMessage message = new TextMessage(ToUserName,fromUserName);
        message.setContent(Content);
        message.setMsgType("text");
        return XStreamUtil.ToXml(message);
    }

    /**
     *
     * 创建一条图文消息
     * 第一个为大图,最多8个,超出删除
     * @author zyf
     * @param ToUserName
     * @param fromUserName
     * @param list
     * @return
     */
    public static String initPicNewsMessage(String ToUserName, String fromUserName, List<PicNews> list) {
        PicNewsMessage message = new PicNewsMessage(ToUserName,fromUserName);
        message.setArticleCount(1);
        if (list.size() > 8) {
            int index = list.size();
            for (int i = index; i >  8; i--) {
                list.remove(i);
            }
        }
        message.setMsgType("news");
        message.setArticles(list);
        return PicNewsMessageToXml(message);
    }


    /**
     *
     * <P>标题：图文消息xml转换</p>
     * @author zyf
     * @param picNewsMessage
     * @return
     */
    public static String PicNewsMessageToXml(PicNewsMessage picNewsMessage){
        XStream xstream = new XStream();
        xstream.alias("xml", picNewsMessage.getClass());
        xstream.alias("item", new PicNews().getClass());//<articles>下根为<items>
        return xstream.toXML(picNewsMessage);
    }

}
